/**
 * The main class of the add on handles windows and graphical stuff 
 * If anything needs to get added to a list or removed this is where it happens
 * it is also responsible for setting up all the event listeners and callbacks 
 *  
 */
function Spiderpig(){
    
    this.attackVectors = new Array();
    this.links = new Array();
    
    this.init = function() {
        try{
            var appcontent = document.getElementById("appcontent");   // browser
            if(appcontent)
               appcontent.addEventListener("DOMContentLoaded", this.onPageLoad, true);
               
            this.initialized = true 
            logHandler("spiderpig is inited");
      
      
      
            //alert("sp init");
            this.settings = new Settings();
            this.fuzzer = new Fuzzer();
            var httpRequestObserver = new RequestObserver();
            var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
            observerService.addObserver(httpRequestObserver, "http-on-modify-request", false);
            observerService.addObserver(httpRequestObserver, "http-on-examine-response", false);
        	observerService.addObserver(httpRequestObserver, "http-on-examine-merged-response", false);        	
        }catch (e){errorHandler(e);}  
    }
    
    
    this.onPageLoad = function(aEvent) {
    var doc = aEvent.originalTarget;
    
    }
    
    this.addToRequestTree = function(req){
        try{
           var reqTreeChildren = document.getElementById("reqTreeChildren");                    
           var item = document.createElement('treeitem');
           var row = document.createElement("treerow");
                       
           var cell = document.createElement('treecell');
           cell.setAttribute('label', req.RequestMethod );
           cell.setAttribute('value', req.RequestMethod );
           row.appendChild(cell);
                       
           var cell = document.createElement('treecell');
           cell.setAttribute('label', req.ResponseStatus );
           cell.setAttribute('value', req.ResponseStatus );
           row.appendChild(cell);
                       
           var cell = document.createElement('treecell');
           cell.setAttribute('label', req.Url );
           cell.setAttribute('value', req.id );
                       
           if(req.RequestMethod == "POST"){
               row.setAttribute('style', 'bg-color: red');
           }
                       
           row.appendChild(cell);
           item.appendChild(row);
           reqTreeChildren.appendChild(item);
                      
           }catch(e){errorHandler(e);}
                       
    }
    
    this.addToExternalTree = function(req){
                       try{
                          
                       var extTreeChildren = document.getElementById("externalReqTreeChildren");                    
                       var item = document.createElement('treeitem');
                       var row = document.createElement("treerow");
                       
                       var cell = document.createElement('treecell');
                       cell.setAttribute('label', req.RequestMethod );
                       cell.setAttribute('value', req.RequestMethod );
                       row.appendChild(cell);
                       
                       var cell = document.createElement('treecell');
                       cell.setAttribute('label', req.ResponseStatus );
                       cell.setAttribute('value', req.ResponseStatus );
                       row.appendChild(cell);
                       
                       var cell = document.createElement('treecell');
                       cell.setAttribute('label', req.Url );
                       cell.setAttribute('value', req.id );
                       
                       if(req.RequestMethod == "POST"){
                           row.setAttribute('style', 'bg-color: red');
                       }
                       
                       row.appendChild(cell);
                       item.appendChild(row);
                       extTreeChildren.appendChild(item);
                      
                       }catch(e){errorHandler(e);}
                       
    }
    
   this.addToLogTree = function(data){
       try{
            var logTreeChildren = document.getElementById("logTreeChildren");                    
            var item = document.createElement('treeitem');
            var row = document.createElement("treerow");
                       
            var cell = document.createElement('treecell');
            var currentTime = new Date();
                       
            cell.setAttribute('label', currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds() );
            cell.setAttribute('value', currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds() );
            row.appendChild(cell);
 
            var cell = document.createElement('treecell');
            cell.setAttribute('label', data );
            cell.setAttribute('value', data );                       
            row.appendChild(cell); 
            item.appendChild(row);
            logTreeChildren.appendChild(item);
                      
           }catch(e){errorHandler(e);}
                       
    }
    
    this.fuzzFromTree = function(tree) {
       try{
           var tree = document.getElementById('requestTree');          
           var id = tree.view.getCellText(tree.currentIndex,tree.columns.getNamedColumn('url'));
           sp.fuzzer.fuzz(this.attackVectors[id], sp.settings.xss);
       }catch(e){errorHandler(e);}
    }
                        
    this.repeatFromTree = function(tree) {
       try{
           var tree = document.getElementById('requestTree');          
           var id = tree.view.getCellText(tree.currentIndex,tree.columns.getNamedColumn('url'));
           // Add tab  
           // Add tab, then make active
           gBrowser.selectedTab = gBrowser.addTab(this.attackVectors[id].Url);
       }catch(e){errorHandler(e);}
    }
    
    this.showSettings = function(){ 
    try {
         
         window.openDialog("chrome://spiderpig/content/options.xul", "test", "chrome,titlebar,toolbar,centerscreen,modal");
         //var WindowObjectReference = window.open("chrome://spiderpig/content/options.xul", "DescriptiveWindowName", "resizable=no,scrollbars=yes,status=no");
        }catch(e){errorHandler(e);}
    } 
    
    this.populateAttackList = function(){
      try{ 
        var list = this.spiderWindow.document.getElementById('attackList');
        list.appendItem(this.attackVectors[0].Url);
      }catch(e){errorHandler(e);}
    }    
     
    this.showWindow = function(){ 
    try {
         
         this.spiderWindow = window.open("chrome://spiderpig/content/spiderpane.xul", "test", "chrome,titlebar,toolbar,centerscreen");
         this.populateAttackList();
         //var WindowObjectReference = window.open("chrome://spiderpig/content/options.xul", "DescriptiveWindowName", "resizable=no,scrollbars=yes,status=no");
        }catch(e){errorHandler(e);}
    }     
}
